package java_thinking.unit_11._11_6;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;

/**
 * @Description :   创建并组装一个List<Integer>,然后按键第二个具有相同尺寸的List<Integer>,并使用ListIterator读取第一个List中的
 *  元素。然后再将他们以反序插入到第二个列表中
 * @author: cww
 * @DateTime: 2019-03-17 17:22
 */

public class E12 {
    static void reverse(List<Integer> list) {
        ListIterator<Integer> fwd = list.listIterator();
        //可以通过调用listIterator(n)方法创建一个一开始就指向列表索引为n的元素处的ListIterator
        //这里表示指向list最后一位元素索引位置
        ListIterator<Integer> rev = list.listIterator(list.size());
        int mid = list.size() >> 1;
        for (int i =0;i<mid;i++){
            //返回下一个元素同时将指针下移一位
            Integer tmp = fwd.next();
            //返回上一个元素并将指针上移一位
            fwd.set(rev.previous());
            rev.set(tmp);
        }
    }

    public static void main(String[] args) {
        List<Integer> src = Arrays.asList(1,2,3,4,5,6,7,8,9,10);
        List<Integer> dest = new LinkedList<>(src);
        System.out.println("source: "+src);
        System.out.println("destination: "+dest);
        reverse(src);
        System.out.println("source: " + src);
        System.out.println("destination: " + dest);
    }

    public void reversWithInner(List<Integer> integerList){
        ListIterator<Integer> it1 = integerList.listIterator();
        ListIterator<Integer> it2 = integerList.listIterator(integerList.size());
        int count = integerList.size() >>1;
        for (int i=0;i<count;i++){
            Integer tmp = it1.next();
            it1.set(it2.previous());
            it2.set(tmp);
        }

    }
    public static class Inner{
        public static void main(String[] args) {
            List<Integer> list = Arrays.asList(1,2,3,4,5,6,7,8,9,10);
            System.out.println("source list: "+list);
            reverse(list);
            System.out.println("after reverse list: "+list);
        }
    }
}
